﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Loki.UI
{
	/// <summary>
	/// Base class for container viewmodels
	/// </summary>
	public class ContainerViewModelBase : ViewModelBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ContainerViewModelBase"/> class.
		/// </summary>
		public ContainerViewModelBase()
		{
			SubModels = new ObservableCollection<ViewModelBase>();
		}

		#region Sub models
		/// <summary>
		/// Gets the sub models.
		/// </summary>
		public ObservableCollection<ViewModelBase> SubModels { get; private set; }
		#endregion

		/// <summary>
		/// Adds the sub model.
		/// </summary>
		/// <param name="P_ViewModel">The view model.</param>
		protected void AddSubModel(ViewModelBase P_ViewModel)
		{
			AddHandler(P_ViewModel.CloseCommand, SubModel_Close_CanExecute, SubModel_Close);
			SubModels.Add(P_ViewModel);
		}

		#region Submodel close
		/// <summary>
		/// Handles the CanExecute event of the SubModel_Close Command.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="Loki.UI.CanExecuteCommandEventArgs"/> instance containing the event data.</param>
		protected virtual void SubModel_Close_CanExecute(object sender, CanExecuteCommandEventArgs e)
		{
			e.CanExecute = true;
		}

		/// <summary>
		/// Handles the Close event of the SubModel command.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="Loki.UI.CommandEventArgs"/> instance containing the event data.</param>
		protected virtual void SubModel_Close(object sender, CommandEventArgs e)
		{
			ViewModelBase L_Model = e.Parameter as ViewModelBase;
			if (L_Model != null)
			{
				SubModels.Remove(L_Model);
				RemoveHandler(L_Model.CloseCommand);
			}
		}
		#endregion
	}
}
